package com.vv.vv_commonlibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.vv.vv_commonlibrary.R;

public class CenterDialog extends Dialog {

    public static final int TYPE_ID_CARD_CHECK = 1;

    public CenterDialog(@NonNull Context context) {
        super(context);
    }

    public CenterDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CenterDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder implements View.OnClickListener {
        private Context context;
        private CenterDialog dialog;
        private View view;

        //预约工作界面弹窗
        private TextView btnTv;
        private TextView titleTv;
        private TextView messageTv;
        private TextView cancelTv;
        private View lineTv;
        private TextView confrimTv;


        //身份证图片认证弹窗
        private TextView idCardNameTv;
        private TextView idCardNumberTv;
        private TextView idCardConfirmTv;
        private TextView idCardCancelTv;

        public Builder(Context context) {
            this.context = context;
            dialog = new CenterDialog(context, R.style.CenterDialogStyle);
            view = LayoutInflater.from(context).inflate(R.layout.dialog_center_layout, null);
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(true);
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.8); //设置宽度
            dialog.getWindow().setAttributes(lp);

            titleTv = (TextView) view.findViewById(R.id.dialog_center_title_tv);
            messageTv = (TextView) view.findViewById(R.id.dialog_center_message_tv);
            cancelTv = (TextView) view.findViewById(R.id.dialog_center_cancel_tv);
            lineTv = (View) view.findViewById(R.id.dialog_center_line);
            confrimTv = (TextView) view.findViewById(R.id.dialog_center_confrim_tv);

            cancelTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnClickListener != null) {
                        onBtnClickListener.onCancel(v);
                    }
                    dialog.cancel();
//                    dialog.dismiss();
                }
            });

            confrimTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnClickListener != null) {
                        onBtnClickListener.onConfirm(v);
                        dialog.dismiss();
                    }
                    if (onConfirmListener != null) {
                        onConfirmListener.onConfirm(v);
                        dialog.dismiss();
                    }
                }
            });
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {

                }
            });
        }

        @Deprecated
        public Builder setLayoutId(int layoutId) {
            view = LayoutInflater.from(context).inflate(layoutId, null);
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.8); //设置宽度
            dialog.getWindow().setAttributes(lp);

            return this;
        }

        public Builder setData(int shoWhat, String... args) {
            switch (shoWhat) {
                case TYPE_ID_CARD_CHECK:
                    idCardNameTv.setText(args[0]);
                    idCardNumberTv.setText(args[1]);
                    break;
            }
            return this;
        }

        public Builder setTitle(String title) {
            titleTv.setText(title);
            titleTv.setVisibility(View.VISIBLE);
            return this;
        }

        public Builder setTitleVisible(int visible) {
            titleTv.setVisibility(visible);
            return this;
        }

        public Builder setMessage(String message) {
            messageTv.setText(message);
            messageTv.setVisibility(View.VISIBLE);
            return this;
        }

        public Builder setMessage(SpannableStringBuilder message) {
            messageTv.setText(message);
            messageTv.setMovementMethod(LinkMovementMethod.getInstance());
            messageTv.setVisibility(View.VISIBLE);
            return this;
        }


        public Builder setCancelVisibility(int visibility) {
            if (cancelTv != null) {
                cancelTv.setVisibility(visibility);
            }
            return this;
        }

        public Builder setCancel(String cancelTxt) {
            cancelTv.setText(cancelTxt);
            cancelTv.setVisibility(View.VISIBLE);
            if (confrimTv.getVisibility() == View.VISIBLE) {
                lineTv.setVisibility(View.VISIBLE);
            } else {
                lineTv.setVisibility(View.GONE);
            }
            return this;
        }

        public Builder setConfrimTextColor(int color) {
            confrimTv.setTextColor(color);
            return this;
        }

        public Builder setCancel(String cancelTxt, OnCancelListener onCancelListener) {
            cancelTv.setText(cancelTxt);
            cancelTv.setVisibility(View.VISIBLE);
            if (confrimTv.getVisibility() == View.VISIBLE) {
                lineTv.setVisibility(View.VISIBLE);
            } else {
                lineTv.setVisibility(View.GONE);
            }
            dialog.setOnCancelListener(onCancelListener);
            return this;
        }

        public Builder setConfrim(String confrimTxt) {
            confrimTv.setText(confrimTxt);
            confrimTv.setVisibility(View.VISIBLE);
            if (cancelTv.getVisibility() == View.VISIBLE) {
                lineTv.setVisibility(View.VISIBLE);
            } else {
                lineTv.setVisibility(View.GONE);
            }
            confrimTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            return this;
        }

        public Builder setConfrimTextSize(int textSize) {
            confrimTv.setTextSize(textSize);
            return this;
        }

        public Builder setConfirmTextColor(@ColorInt int color) {
            confrimTv.setTextColor(color);
            return this;
        }

        public Builder setConfrim(String confrimTxt, OnConfirmListener onConfrimListener) {
            confrimTv.setText(confrimTxt);
            confrimTv.setVisibility(View.VISIBLE);
            if (cancelTv.getVisibility() == View.VISIBLE) {
                lineTv.setVisibility(View.VISIBLE);
            } else {
                lineTv.setVisibility(View.GONE);
            }
            this.onConfirmListener = onConfrimListener;
            return this;
        }

        public Builder setConfrim(String confrimTxt, OnBtnClickListener onBtnClickListener) {
            confrimTv.setText(confrimTxt);
            confrimTv.setVisibility(View.VISIBLE);
            if (cancelTv.getVisibility() == View.VISIBLE) {
                lineTv.setVisibility(View.VISIBLE);
            } else {
                lineTv.setVisibility(View.GONE);
            }
            this.onBtnClickListener = onBtnClickListener;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            dialog.setCancelable(cancelable);
            return this;
        }


        public CenterDialog builder() {
            return dialog;
        }

        @Override
        public void onClick(View view) {

        }


        private OnBtnClickListener onBtnClickListener;

        public Builder setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
            this.onBtnClickListener = onBtnClickListener;
            return this;
        }

        public Builder setCancelTextColor(int color) {
            this.cancelTv.setTextColor(color);
            return this;
        }

        public interface OnBtnClickListener {
            void onCancel(View v);

            void onConfirm(View v);
        }

        private OnConfirmListener onConfirmListener;

        public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
            this.onConfirmListener = onConfirmListener;
        }

        public interface OnConfirmListener {
            void onConfirm(View v);
        }
    }

}
